package com.item.web.image;

import com.item.utils.ResultUtils;
import com.item.utils.ResultVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;


@RestController
@RequestMapping("/api/upload/")
@Api(tags = "图片上传模块")
public class ImageUploadController {
     //图片上传的路径
    @Value("${web.uploadpath}")
    private String webUploadpath;

    @RequestMapping("/uploadImage")
    public ResultVo uploadImage(@RequestParam("file") MultipartFile file){
        String Url = null;
        //获取文件名称
        String filename = file.getOriginalFilename();
        //获取扩展名  aa.png
        String fileExtenionName = filename.substring(filename.indexOf("."));
        //生成新的文件名
        String newName = UUID.randomUUID().toString()+fileExtenionName;
        String path = webUploadpath;
        File fileDir = new File(path);
        if(!fileDir.exists()){
            //设置权限
            fileDir.setWritable(true);
            fileDir.mkdirs();
        }
        File targetFile = new File(path,newName);
        try{
            file.transferTo(targetFile);
            Url = "/"+targetFile.getName();
        }catch (Exception e){
            return null;
        }
        return ResultUtils.success("成功","/images" + Url);
    }
}
